package com.lczc.soa.controller;

import com.lczc.soa.domain.Policy;
import com.lczc.soa.domain.SearchApplyResponse;
import com.lczc.soa.domain.SearchPolicyResponse;
import com.lczc.soa.service.PolicyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/Policy")
public class PolicyController {

    @Autowired
    private PolicyService policyService;

    @RequestMapping("/add")
    public String toAddPolicy(Model model){
        model.addAttribute("policy",new Policy());
        return "policy_add";
    }

    @RequestMapping("/save")
//    @ResponseBody
    public String SavePolicy(HttpServletRequest request, Policy policy){
        policy.setPolicyName(request.getAttribute("policyName").toString());
        policy.setPolicyAuthor(request.getAttribute("policyAuthor").toString());
        policy.setPolicyContent(request.getAttribute("policyContent").toString());
//        policy.setPolicyId(UUID.randomUUID().toString());
        int updated = policyService.savePolicy(policy);
        return "redirect:/Policy/list";
    }

    @RequestMapping("/list")
    public String PolicyList(){
        return "policy_list";
    }

    @GetMapping("/api/search")
    @ResponseBody
    public SearchPolicyResponse searchPolicyResponse(@RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize){
        return policyService.findAllPolicy(pageNum,pageSize);
    }

    @GetMapping("/api/searchLike")
    @ResponseBody
    public SearchPolicyResponse searchPolicyResponseLike(@RequestParam("keywords") String keywords,
                                                         @RequestParam("pageNum") Integer pageNum,
                                                         @RequestParam("pageSize") Integer pageSize){
        return policyService.findPolicyLikeName(keywords,pageNum,pageSize);
    }

    @RequestMapping("/edit")
    public String EditPolicy(@RequestParam("id") String id, Model model){
        Policy policy = policyService.findPolicyByPrimaryKey(id);
        model.addAttribute("policy",policy);
        return "policy_add";
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deletePolicy(@RequestParam("id") String id){
        int deleted = policyService.deletePolicy(id);
        return deleted > 0;
    }

    @RequestMapping("/page")
    public String PolicyPage(Model model){
        List<Policy> policies = policyService.listPolicy();
        model.addAttribute("policies",policies);
        return "policy_page";
    }
    @RequestMapping("/content")
    public String PolicyContent(@RequestParam("id") String id,Model model){
        Policy policy = policyService.findPolicyByPrimaryKey(id);
        model.addAttribute("policy",policy);
        return "policy_content";
    }
}
